# coding : utf-8

version = (1, 0, 0)
VERSION = "%d.%d.%d" % version

__author__ = "sihanfei <sheng.tao@superg.ai>"
__doc__ = """A HDMap base elements define liberary."""

__all__ = ['map_line', 'map_polygon', 'map_patches', 'map_command_def']

import numpy as np
import dxfgrabber

from . import attribute_def
from . import map_command_def
from . import map_line
from . import map_patches
from . import map_polygon


def getEntityTrueColor(layers, entity):
    """
    get the true_color of the dxf_entity in dxf file
    input:
    @file: the layers of the dxf file opened with dxfgrabber
    @entity: the entity in dxf_file.entities
    return:
    @true_color: the color value in matplotlib format, just like (0.1, 0.1, 0.1)
    """
    color = dxfgrabber.color.dxf_default_colors[layers[entity.layer].color]
    true_color = (((color & 0xFF0000) >> 16) / 255,
                  ((color & 0x00FF00) >> 8) / 255,
                  ((color & 0x0000FF) >> 0) / 255)
    return true_color


def getEntityTrueLineType(layers, entity):
    """
    get the true_linetype of the dxf_entity in dxf file
    input:
    @file: the layers of the dxf file opened with dxfgrabber
    @entity: the entity in dxf_file.entities
    return:
    @true_linetype: the line_type value in matplotlib format
    """
    ture_linetype = layers[entity.layer].linetype
    return ture_linetype


def calcPointsDistance(p0, p1):
    return (np.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2))


def calcAngle2Lines(l0, l1):
    """
    计算L0(p0, p1)与L1(p2, p3)两条直线的夹角
    以L1为基准，L0向L1旋转，逆时针为正，顺时针为负
    """
    angle = 0
    theta0 = np.arctan2(l0[1][1] - l0[0][1], l0[1][0] - l0[0][0])
    theta1 = np.arctan2(l1[1][1] - l1[0][1], l1[1][0] - l1[0][0])
    angle = theta1 - theta0
    if angle > np.pi * 2:
        angle -= np.pi * 2
    elif angle < 0:
        angle += np.pi * 2
    return angle

COMMAND_DICT = {'参考线':['选择参考线', '选择起点', '选择边界线', '组合一条边界线', '生成参考点'], \
                '附属物':['选择边界点', '设置属性', ''], \
                '缓行线':['选择起始线', '选择终止线', '计算']}

MAP_TYPE_DEF = ['appendix', 'ref', 'board']

MAP_TYPE_COMMAND_COUNT_DICT = {
    MAP_TYPE_DEF[0]: 2,
    MAP_TYPE_DEF[1]: 4,
    MAP_TYPE_DEF[2]: 2
}

if __name__ == "__main__":
    l0 = np.array([(0, 0), (1, 0)])
    l1 = np.array([(0, 0), (1, -1)])
    angle = calcAngle2Lines(l0, l1)
    print('angle = {}'.format(angle * 180 / np.pi))
